(function($) {


function Initialise(urls, showEmail) {
    var self = this;

    var default_email = "Email address (optional - only necessary if you would like us to contact you)";
    if (showEmail) {
        $('#id_feedback_dialog_email_address').val(default_email);
        $('#id_feedback_dialog_email_address').addClass('default_prompt');
        $('#id_feedback_dialog_email_address').focus(function() {
            $('#id_feedback_dialog_email_address').removeClass('default_prompt');
            if ($('#id_feedback_dialog_email_address').val() == default_email) {
                $('#id_feedback_dialog_email_address').val('');
            }
        });
    } else {
        $('#id_feedback_dialog_email_address').hide();
    }

    $('.feedback_link').click(function(event) {
        $('#id_feedback_dialog_error').addClass('pa_hidden');
        $('#id_feedback_dialog_spinner').addClass('pa_hidden');
        $('#id_feedback_dialog_ok_button').removeAttr('disabled');
        $('#id_feedback_dialog_cancel_button').removeAttr('disabled');
        $("#id_feedback_dialog_text").val('');
        $('#id_feedback_dialog').dialog({
                width: 610
        });
        event.preventDefault();
    });

    $('#id_feedback_dialog_ok_button').click(function() {
        var data = {
            message: $('#id_feedback_dialog_text').val(),
            email_address: $('#id_feedback_dialog_email_address').val(),
            referring_url: window.location.href
        };
        Anywhere.Utils.DoAjaxWithSpinnerAndErrorsAndButtonDisabling({
            url: urls.feedback,
            data: data,
            spinner: '#id_feedback_dialog_spinner',
            error: '#id_feedback_dialog_error',
            buttonsToDisable: ['#id_feedback_dialog_ok_button', '#id_feedback_dialog_cancel_button'],
            onSuccessFunction: _closeDialog
        });

    });

    $('#id_feedback_dialog_cancel_button').click(function() {
        $('#id_feedback_dialog_error').addClass('pa_hidden');
        $('#id_feedback_dialog').dialog("close");
    });

}

function _closeDialog () {
    $('#id_feedback_dialog').dialog("close");
}
$.extend(true, window, {
    "Anywhere": {
        "FeedbackDialog": {
            "Initialise": Initialise,
            "_closeDialog": _closeDialog
        }
    }
});

})(jQuery);
